/*   Shotscreens
 *   Copyright (C) 2010, Vadim Zakondyrin (http://crystalnix.com)
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "qgraphicsarrowitem.h"
#define ARROW_ANGLE 150

QGraphicsArrowItem::QGraphicsArrowItem(const QPen & pen, QGraphicsItem *parent)
                                           : QGraphicsItemGroup(parent)
{
    initializeArrow(pen);
}

QGraphicsArrowItem::QGraphicsArrowItem(const QLineF & line,
                                       const QPen & pen, QGraphicsItem *parent)
                                           : QGraphicsItemGroup(parent)
{
    initializeArrow(pen);
    setLine(line);
}

QGraphicsArrowItem::QGraphicsArrowItem(qreal x1, qreal y1, qreal x2, qreal y2,
                                       const QPen &pen, QGraphicsItem *parent)
                                           : QGraphicsItemGroup(parent)
{
    initializeArrow(pen);
    setLine(x1, y1, x2, y2);
}

QGraphicsArrowItem::~QGraphicsArrowItem()
{

}

void QGraphicsArrowItem::setPen(const QPen & pen)
{
    QPen newPen(pen);
    newPen.setCapStyle(Qt::RoundCap);
    leftLine->setPen(newPen);
    rightLine->setPen(newPen);
    mainLine->setPen(newPen);
}

void QGraphicsArrowItem::setLine(const QLineF & line)
{
    mainLine->setLine(line);

    QPointF endPoint = line.p2();
    QPointF thirdFromEnd = line.pointAt(2.0/3.0);
    qreal mainLineAngle = line.angle();

    QLineF left(endPoint, thirdFromEnd);
    left.setAngle(mainLineAngle + ARROW_ANGLE);
    leftLine->setLine(left);

    QLineF right(endPoint, thirdFromEnd);
    right.setAngle(mainLineAngle - ARROW_ANGLE);
    rightLine->setLine(right);
}

void QGraphicsArrowItem::initializeArrow(const QPen & pen)
{
    leftLine = new QGraphicsLineItem(this);
    rightLine = new QGraphicsLineItem(this);
    mainLine = new QGraphicsLineItem(this);
    setPen(pen);

    qDebug("QGraphicsArrowItem constructed");
}

QRectF QGraphicsArrowItem::boundingRect() const
{
    QRectF rect = mainLine->boundingRect();
    updateBoundingRectByRect(rect, leftLine->boundingRect());
    updateBoundingRectByRect(rect, rightLine->boundingRect());
    return rect;
}

void QGraphicsArrowItem::updateBoundingRectByRect(QRectF &rect, const QRectF &anotherRect) const
{
    if (rect.bottom() < anotherRect.bottom()) {
        rect.setBottom(anotherRect.bottom());
    }

    if (rect.top() > anotherRect.top()) {
        rect.setTop(anotherRect.top());
    }

    if (rect.left() > anotherRect.left()) {
        rect.setLeft(anotherRect.left());
    }

    if (rect.right() < anotherRect.right()) {
        rect.setRight(anotherRect.right());
    }
}
